package com.smartcruise.start.modifyPassword;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;

import com.smartcruise.R;
import com.smartcruise.base.BaseTitleFragment;
import com.smartcruise.base.TitleBuilder;
import com.smartcruise.util.IntentUtils;
import com.smartcruise.util.customView.ClearableEditTextWithIcon;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FengChaoQun
 * on 2018/3/17 0017
 * 修改密码界面
 */

public class ModifyPasswordFragment extends BaseTitleFragment implements ModifyPasswordContract.View {
    @BindView(R.id.password)
    ClearableEditTextWithIcon password;
    @BindView(R.id.password2)
    ClearableEditTextWithIcon password2;
    @BindView(R.id.submit)
    Button submit;

    private boolean findPassword;//标记是否是通过验证码找回密码
    private String phone;
    private ModifyPasswordContract.Presenter mPresenter;

    public static ModifyPasswordFragment getInstance(boolean findPassword, String phone) {
        ModifyPasswordFragment modifyPasswordFragment = new ModifyPasswordFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(IntentUtils.TYPE, findPassword);
        bundle.putString(IntentUtils.DATA, phone);
        modifyPasswordFragment.setArguments(bundle);
        return modifyPasswordFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findPassword = getArguments() != null && getArguments().getBoolean(IntentUtils.TYPE);
        phone = getArguments() != null ? getArguments().getString(IntentUtils.DATA) : null;
    }

    @Override
    protected void init() {
        mPresenter = new MofidyPasswordPresenterImp(this, findPassword);
        mPresenter.start();

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (password.length() >= 6 && password2.length() >= 6 && password.getText().toString().equals(password2.getText().toString())) {
                    submit.setAlpha(1f);
                } else {
                    submit.setAlpha(0.5f);
                }
            }
        };
        password.addTextChangedListener(textWatcher);
        password2.addTextChangedListener(textWatcher);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_contain_set_password;
    }

    @Override
    protected TitleBuilder getTitleBuilder() {
        return TitleBuilder.create(true, getString(R.string.setPassword));
    }

    @OnClick(R.id.submit)
    public void onViewClicked() {
        if (password.length() >= 6 && password2.length() >= 6 && password.getText().toString().equals(password2.getText().toString())) {
            mPresenter.clickOnSubmit();
        } else if (password.length() < 6 || password2.length() < 6) {
            showToast("密码长度不得少于6位");
        } else {
            showToast("两次输入的密码内容不同");
        }
    }

    @Override
    public String getPhone() {
        return phone;
    }

    @Override
    public String getPassword() {
        return password.getText().toString();
    }
}
